package com.chen.solution.rpc.account.svc.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.chen.solution.rpc.account.api.dto.AccountDto;
import com.chen.solution.rpc.account.api.dto.AccountList;
import com.chen.solution.rpc.account.svc.model.Account;
import com.chen.solution.rpc.account.svc.repository.AccountRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author ChenTian
 * @date 2022/4/10
 */
@Slf4j
@Service
public class AccountService {

    @Resource
    private AccountRepository accountRepository;


    public AccountDto create(String name, String email, String phoneNumber) {
        Account account = accountRepository.createAccount(name, email, phoneNumber);
        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }

    public AccountDto getOrCreate(String name, String email, String phoneNumber) {
        Account account = accountRepository.getAccountByName(name);
        if(account == null){
            account = accountRepository.createAccount(name, email, phoneNumber);
        }
        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }

    public AccountList list(int offset, int limit) {
        List<Account> accounts = accountRepository.getAccounts(offset, limit);

        List<AccountDto> accountDtoList = BeanUtil.copyToList(accounts, AccountDto.class);

        AccountList accountList = AccountList.builder()
                .accounts(accountDtoList)
                .offset(offset)
                .limit(limit).build();
        return accountList;
    }


    public AccountDto get(String userId) {
        Account account = accountRepository.getAccountByUserId(userId);
        if (account == null){
            return null;
        }

        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }


    public AccountDto getAccountByPhoneNumber(String phoneNumber) {

        Account account = accountRepository.getByPhoneNumber(phoneNumber);
        if(account == null){
            return null;
        }

        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }

    public AccountDto update(AccountDto newAccount) {
        Account account = BeanUtil.toBean(newAccount, Account.class);

        accountRepository.update(account);

        AccountDto accountDto = BeanUtil.toBean(account, AccountDto.class);
        return accountDto;
    }

    public void updatePassword(String userId, String password) {
        Account account = accountRepository.getAccountByUserId(userId);
        if (account == null){
            return;
        }

        log.info("update password hash by id!");
    }



}
